package cn.stylefeng.guns.modular.shuheng.controller;

import cn.stylefeng.guns.core.common.constant.Constants;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.shuheng.service.ContentService;
import cn.stylefeng.guns.modular.shuheng.service.DataService;
import cn.stylefeng.guns.modular.shuheng.service.LevelService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 任务提醒
 */
@Controller
@RequestMapping("/tip")
public class TipController extends BaseController {
    private String PREFIX = "/modular/shuheng/tip/";

    @Autowired
    private LevelService levelService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private DataService dataService;

    @RequestMapping("")
    public String index(Model model){
        ShiroUser user = ShiroKit.getUser();
        Integer year = DateUtil.getNowYear();
        Integer month = DateUtil.getNowMonth();

        List<Map<String,Object>> list = getDataList(year,user,month);
        model.addAttribute("month",month);
        model.addAttribute("list",list);
        return PREFIX + "tip.html";
    }

    @RequestMapping("/notification")
    @ResponseBody
    public Map<String,Object> notification(){
        ShiroUser user = ShiroKit.getUser();
        Integer year = DateUtil.getNowYear();
        Integer month = DateUtil.getNowMonth();
        List<Map<String,Object>> dataList = dataService.listByYearBeforeMonth(year,user,month);
        Map<String,Object> data = new HashMap<>();
        if(dataList!=null){
            data.put("count",dataList.size());
            data.put("months",months(dataList));
        }else{
           data.put("count", Constants.INT_ZERO);
        }
        return data;
    }

    /**
     * 月份 去重
     * @param dataList
     * @return
     */
    private List<String> months(List<Map<String,Object>> dataList){
        List<Integer> months = new ArrayList<>();
        List<String> monthsStr = new ArrayList<>();
        for(Map<String,Object> map:dataList){
            Integer month = (Integer) map.get("data_month");
            if(!months.contains(month)){
                months.add(month);
                monthsStr.add(month + "月");
            }
        }
        return monthsStr;
    }

    private List<Map<String,Object>> getDataList(Integer year,ShiroUser user,Integer month){
        List<Map<String,Object>> dataList = dataService.listByYearMonth(year,user,month);
        List<Map<String,Object>> levelCountList = dataService.countByLevelId(year,user,month);
        List<Map<String,Object>> contentCountList = dataService.countByContentId(year,user,month);
        if(dataList!=null && dataList.size()>0){
            for(int i = 0;i<dataList.size();i++){
                dataList.get(i).put("data_num",num2Small(i+1));
                if(levelCountList!=null && levelCountList.size()>0){
                    for(int j=0;j<levelCountList.size();j++){
                        if(((long)dataList.get(i).get("level_id"))==((long)levelCountList.get(j).get("level_id"))){
                            dataList.get(i).put("level_count",levelCountList.get(j).get("count"));
                            dataList.get(i).put("level_num",num2Big(j+1));
                            levelCountList.remove(j);
                            Map<String,Object> map = new HashMap<>();
                            map.put("level_id",1L);
                            map.put("count",0);
                            levelCountList.add(j,map);
                        }
                    }
                }
                if(contentCountList!=null && contentCountList.size()>0){
                    for(int k=0;k<contentCountList.size();k++){
                        if(((long)dataList.get(i).get("content_id"))==((long)contentCountList.get(k).get("content_id"))){
                            dataList.get(i).put("content_count",contentCountList.get(k).get("count"));
                            dataList.get(i).put("content_num",k+1);
                            contentCountList.remove(k);
                            Map<String,Object> map = new HashMap<>();
                            map.put("content_id",1L);
                            map.put("count",0);
                            contentCountList.add(k,map);
                        }
                    }
                }
            }
        }
        return dataList;
    }


    private String num2Big(int num){
        switch (num){
            case 1:return "一";
            case 2:return "二";
            case 3:return "三";
            case 4:return "四";
            case 5:return "五";
            case 6:return "六";
            case 7:return "七";
            case 8:return "八";
            case 9:return "九";
            case 10:return "十";
            default:return num+"";
        }

    }

    public static String num2Small(int num){
        switch (num){
            case 1:return "①";
            case 2:return "②";
            case 3:return "③";
            case 4:return "④";
            case 5:return "⑤";
            case 6:return "⑥";
            case 7:return "⑦";
            case 8:return "⑧";
            case 9:return "⑨";
            case 10:return "⑩";
            case 11:return "⑪";
            case 12:return "⑫";
            case 13:return "⑬";
            case 14:return "⑭";
            case 15:return "⑮";
            case 16:return "⑯";
            case 17:return "⑰";
            case 18:return "⑱";
            case 19:return "⑲";
            case 20:return "⑳";
            case 21:return "㉑";
            case 22:return "㉒";
            case 23:return "㉓";
            case 24:return "㉔";
            case 25:return "㉕";
            case 26:return "㉖";
            case 27:return "㉗";
            case 28:return "㉘";
            case 29:return "㉙";
            case 30:return "㉚";
            case 31:return "㉛";
            case 32:return "㉜";
            case 33:return "㉝";
            case 34:return "㉞";
            case 35:return "㉟";
            case 36:return "㊱";
            case 37:return "㊲";
            case 38:return "㊳";
            case 39:return "㊴";
            case 40:return "㊵";
            case 41:return "㊶";
            case 42:return "㊷";
            case 43:return "㊸";
            case 44:return "㊹";
            case 45:return "㊺";
            case 46:return "㊻";
            default:return num+"";
        }

    }
}
